package linkedlist;

import java.util.PriorityQueue;

/**
 * @author justnow
 * Created on 2020-10-07
 * Description
 */
public class Leetcode23 {

    public ListNode mergeKLists(ListNode[] lists) {
        PriorityQueue<ListNode> minHeap = new PriorityQueue<ListNode>((o1, o2) -> {
            return o1.val - o2.val;
        });
        for (ListNode list : lists) {
            if (list != null) {
                minHeap.add(list);
            }

        }
        ListNode dumpy = new ListNode(-1);
        ListNode p = dumpy;
        while (!minHeap.isEmpty()) {
            p.next = minHeap.poll();
            p = p.next;
            if (p.next != null) {
                minHeap.add(p.next);
            }
        }
        return dumpy.next;
    }

}
